package yxy.game.pm2.command.camptower;

import com.alibaba.fastjson.JSONArray;
import yxy.cherry.battle.module.Position;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;

import java.util.Collection;
import java.util.Map;

/**
 * 阵营塔布阵界面请求
 *
 * @ClassName CMD_CampTowerGetLineupInfo
 * @Author jingtao-zheng
 * @Date 2022/7/28 19:42
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerGetLineupInfo, classtype = CampTowerMessage.CampTowerGetLineupInfoRequest.class)
public class CMD_CampTowerGetLineupInfo extends Command<CampTowerMessage.CampTowerGetLineupInfoRequest> {
    //不刷新战力
    final  static  int NO_AFRESH_POWER = 1;
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerGetLineupInfoRequest request) throws Exception {
        CampTowerMessage.CampTowerGetLineupInfoResponse.Builder response = CampTowerMessage.CampTowerGetLineupInfoResponse.newBuilder();
        String playerCode = messageSource.getTag();
        //获取阵营类型
        int typeNumber = request.getType().getNumber();
        Format.FormatType formatType = Format.FormatType.CampTowerForNumber(typeNumber);
        if (formatType == null) send(messageSource, response, Result.ParamError);
        //获取玩家所有武将信息
        HeroHandle module = new HeroHandle();
        Collection<Hero> playerAllHero = module.getHeros(playerCode);
        for (Hero hero : playerAllHero) {
            try {
                response.addHeros(module.getHeroSimple(playerCode, hero));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //获取玩家阵容
        FormatHandle formatHandle = new FormatHandle();
        Format format = formatHandle.get(playerCode, formatType);
        CampTowerMessage.Lineup lineups = formatHandle.getCampTowerFormat(format);
        response.setLineup(lineups);
        //上阵队伍总战力
        Map<Position, Hero> map = new FormatHandle().formatHeros(playerCode, formatType);
        JSONArray campTowerAllPower = module.getCampTowerAllPower(playerCode, map, format, formatType, NO_AFRESH_POWER);
        response.setHerosJsonArrary(campTowerAllPower.toString());
        //获取神器
        NiuBeeModule niuBeeModule = NiuBeeModule.get(playerCode);
        response.setNbAct(niuBeeModule.getProgress());
        response.setNbLevel(niuBeeModule.getLevel());
        response.setNbSkill(niuBeeModule.getSkillLevel());
        //返回
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerGetLineupInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
